Skip to content

Theme detection and update improvements #237

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jan 13, 2024

Conversation

Chris-N-K
Copy link
Contributor

@Chris-N-K Chris-N-K commented Nov 30, 2023

Hi,
as mentioned in the issue #236 I did a first draft of a possible implementation of dynamic styling based on the napari viewer theme.

This rework completely omits the concept of static style sheets for the different napari theme and instead translates the current viewer theme into a matplotlib compatible style dictionary.

This happens upon widget initiation and the style dictionary is stored in the attribute: napari_theme_style_sheet.
The style dictionary can be directly used for a motplotlib.style.contect.
Additionally, I fixed the viewer theme callback and the style change upon theme change works now properly.

I encountered some strange during the rework, if I set the facecolor of the figure or axes to something other than transparent (none) the color will stay. Seems like the facecolors are not updated by redraws. I'm feel this is a matplotlib bug, but I could be wrong, maybe I just used the wrong functions for redrawing the figure and axis faces.
EDIT:
I can reproduce this behaviour with pure matplotlib, if I explicitly set the facecolor and redraw it changes, but if I use a style context it stays the same.

If you find the implementation suitable you or I would need to give the test an update as well.

@dstansby
Copy link
Member

dstansby commented Jan 7, 2024

pre-commit.ci autofix

@dstansby
Copy link
Member

dstansby commented Jan 7, 2024

I really like this approach! Not having to deal with custom matplotlib style sheet paths makes life a lot simpler for us, and also gives the user only one place to customise the theme instead of having to do it for both napari and napari-matplotlib. I need to do some manual testing, but in the meantime would you be happy chaging either the code or tests to make the tests pass? If you're confused by the image tests (they are confusing!) just let me know and I can take a look at updating them.

I automatically fixed formatting issues, so there's now an extra commit on your branch. If you want to do taht yourself locally you can install and run pre-commit: https://pre-commit.com/

@dstansby dstansby added the New feature New feature or request label Jan 7, 2024
@dstansby dstansby added this to the 2.0 milestone Jan 7, 2024
@Chris-N-K
Copy link
Contributor Author

Hi, I'm happy you like my suggestions. I can take a look at the tests but I'm rather short on time at the moment so this could take some time.
If you are fine with taking a look at the image tests, I would go through the rest.

@dstansby
Copy link
Member

I've updated the image tests, and cleaned out the old styling code that isn't needed any more. I still need to debug some of the new images, as the text colour doesn't look right. Manual testing indicates this is looking good otherwise though 👍

Comment on lines -57 to -59
# callback to update when napari theme changed
# TODO: this isn't working completely (see issue #140)
# most of our styling respects the theme change but not all
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixes #140 ?

Copy link
Contributor Author

@Chris-N-K Chris-N-K Jan 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes it fixes the axelabel text colour issue, but is kind of a work around for the background issue.
All the text on the canvas (except in he legend) is now linked to the napari text colour which should always be high contrast to the background colour.
The background is transparent, which is not optimal in all cases, but for what ever reason I was not able to implement it in a way that the background is updated correctly.
I guess there might be an underlying bug not directly connected to napari-matplotlib but either napari or and this is my guess the way matplotlib updates the canvas.

@Chris-N-K
Copy link
Contributor Author

Looks like all the tests work now. Is there still something I should check?

@dstansby
Copy link
Member

dstansby commented Jan 13, 2024

pre-commit.ci autofix

@dstansby
Copy link
Member

Nope, I think this all looks good now 👍 . I'll wait for the tests to pass again then merge. Will try and get this in a new release in the next few weeks or so, thanks a lot for the PR!

@dstansby dstansby enabled auto-merge January 13, 2024 12:39
@dstansby dstansby merged commit c481207 into matplotlib:main Jan 13, 2024
@Chris-N-K Chris-N-K deleted the theme_detection_update branch January 14, 2024 21:33
@Chris-N-K
Copy link
Contributor Author

Happy I could help ^^

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
New feature New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants